package mine.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.orm.SugarRecord;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.UIHandler;

import java.util.ArrayList;
import java.util.List;

import mine.adapter.NoteBookAdapter;
import model.NoteBook;

/**
 * 记事本
 * Created by Administrator on 2016/3/19.
 */
public class NoteBookActivity extends UIHandler.BaseActivity {

    Button btn_note;
    ListView lv_simplenote;
    List<NoteBook> noteBooks = new ArrayList<NoteBook>();
    NoteBookAdapter noteBookAdapter = new NoteBookAdapter();
    {
        noteBookAdapter.setNoteBooks(noteBooks);
    }



    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == btn_note)
            {
                Intent intent = new Intent(NoteBookActivity.this,NoteTheThingActivity.class);
                NoteBookActivity.this.startActivity(intent);
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notebook);
        initview();
    }

    @Override
    protected void onStart() {
        super.onStart();
        initdata();
    }

    private void initdata() {
        List<NoteBook> noteBooksTmp = SugarRecord.findWithQuery(NoteBook.class,"select * from notebook order by time desc");
        noteBooks.clear();
        noteBooks.addAll(noteBooksTmp);
        noteBookAdapter.notifyDataSetChanged();
    }

    // 初始化界面
    private void initview() {
        btn_note = (Button) findViewById(R.id.id_btn_note);
        btn_note.setOnClickListener(onClickListener);
        lv_simplenote = (ListView) findViewById(R.id.id_lv_simplenote);
        lv_simplenote.setAdapter(noteBookAdapter);
        lv_simplenote.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                NoteBook noteBook = (NoteBook) view.getTag(R.id.id_notebook);
                Intent intent = new Intent(NoteBookActivity.this,NoteReWriteActivity.class);
                intent.putExtra("notebook",noteBook);
                NoteBookActivity.this.startActivity(intent);

            }
        });
    }
}
